import express from "express"
import dotenv from "dotenv"
import bodyParser from 'body-parser'
import cookieParser from "cookie-parser"
import { v2 as cloudinary } from "cloudinary"



import putoutRoutes from "./routes/putout.route.js"
import authRoutes from "./routes/auth.route.js"
import connectMongoDB from "./db/connectMongoDb.js"
import userRoutes from "./routes/user.route.js"
import notificationRoutes from "./routes/notification.route.js"



// setup dotenv for .env file
dotenv.config()
// setup cloudinary, the other website 
// TODO: fix this
cloudinary.config({
    cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
    api_key: process.env.CLOUDINARY_API_KEY,
    api_secret: process.env.CLOUDINARY_API_SECRET,
})



const app = express()
const PORT = process.env.PORT || 6969 // TODO: config me

console.log(process.env.MONGO_URI)

// use middleware
// REF LINK: https://blog.csdn.net/u013438638/article/details/48953143
// app.use(express.json({ limit: "5mb" })); // to parse req.body
app.use(bodyParser.json({ limit: "5mb" }))// limit should not be too high to prevent DDos
// app.use(express.urlencoded({extended: true}))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())


// NOTE:use auth route, use middleware first then use route group
app.use("/api/auth", authRoutes)
app.use("/api/putouts", putoutRoutes)
app.use("/api/users", userRoutes)
app.use("/api/notifications", notificationRoutes)



// backend entry point
app.listen(PORT, () => {
    console.log(`server: http://localhost:${PORT}`)
    connectMongoDB()
})

